<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		请求得到的数据：<span></span><br>
		<button type="button" onclick="fun()">同步请求</button>
		<button type="button" onclick="fun2()">异步请求</button>
	</body>
	<script type="text/javascript">
		function fun(){
			//1创建核心XMLHttpRequest对象
			let xhr = new XMLHttpRequest();
			//2创建请求
			xhr.open("get","data.txt",false);//false表示同步
			//3发送请求
			xhr.send();
			//4处理数据
			let txtdata = xhr.responseText;//获得文本数据
			document.querySelector("span").innerHTML = txtdata;
		}
		function fun2(){
			//1创建核心XMLHttpRequest对象
			let xhr = new XMLHttpRequest();
			//2创建请求
			xhr.open("get","data.txt",true);//true表示异步
			//3发送请求
			xhr.send();
			//4处理数据
			xhr.onreadystatechange = function(){
				if(xhr.readyState == 4 && xhr.status == 200){
					let txtdata = xhr.responseText;//获得文本数据
					document.querySelector("span").innerHTML = txtdata;
				}
			}
		}
	</script>
</html>
